package com.running.base.run.jms.sender.config;


import com.running.base.run.jms.sender.WeblogicSecureThreadFactorySender;
import com.running.base.run.jms.sender.WeblogicSecurityBeanSender;
import com.running.base.run.jms.sender.WeblogicSecurityInterceptStrategySender;
import org.apache.camel.component.jms.JmsConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.destination.JndiDestinationResolver;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.jndi.JndiTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.jms.ConnectionFactory;
import java.util.Properties;

@Configuration
@ConditionalOnProperty(value = "jms.param", havingValue = "true")
public class JMSSenderConfig {

    @Value("${jms.sender.providerUrl}")
    private String jmsSenderProviderUrl;

    @Value("${jms.sender.username}")
    private String jmsSenderUsername;

    @Value("${jms.sender.password}")
    private String jmsSenderPassword;

    @Value("${jms.sender.connectionFactory}")
    private String jmsSenderConnectionFactory;

    @Value("${jms.sender.destination}")
    private String jmsSenderDestination;

    @Bean("weblogicSecurityBeanSender")
    public WeblogicSecurityBeanSender weblogicSecurityBeanSender() {
        WeblogicSecurityBeanSender weblogicSecurityBeanSender = new WeblogicSecurityBeanSender();
        weblogicSecurityBeanSender.setProviderUrl(jmsSenderProviderUrl);
        weblogicSecurityBeanSender.setSecurityPrincipal(jmsSenderUsername);
        weblogicSecurityBeanSender.setSecurityCredentials(jmsSenderPassword);
        return weblogicSecurityBeanSender;
    }

    @Bean("weblogicThreadFactorySender")
    public WeblogicSecureThreadFactorySender weblogicSecureThreadFactorySender() {
        WeblogicSecureThreadFactorySender weblogicSecureThreadFactorySender = new WeblogicSecureThreadFactorySender();
        weblogicSecureThreadFactorySender.setName("jmsSubscribers");
        weblogicSecureThreadFactorySender.setWeblogicSecurityBeanSender(weblogicSecurityBeanSender());
        return weblogicSecureThreadFactorySender;
    }

    @Bean("jmsTaskExecutorSender")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setBeanName("jmsTaskExecutorSender");
        threadPoolTaskExecutor.setThreadFactory(weblogicSecureThreadFactorySender());
        threadPoolTaskExecutor.setCorePoolSize(1);
        threadPoolTaskExecutor.setQueueCapacity(0);
        return threadPoolTaskExecutor;
    }


    /**
     * jms.sender.connectionFactory=ConnectionFactory-FD
     * jms.sender.destination=jn_fd_queue
     * @return
     */

    @Bean("jndiTemplateSender")
    public JndiTemplate jndiTemplate() {
        JndiTemplate jndiTemplate = new JndiTemplate();
        Properties environment = new Properties();
        environment.setProperty("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
        environment.setProperty("java.naming.provider.url", jmsSenderProviderUrl);
        environment.setProperty("java.naming.security.principal", jmsSenderUsername);
        environment.setProperty("java.naming.security.credentials", jmsSenderPassword);
        jndiTemplate.setEnvironment(environment);
        return jndiTemplate;
    }

    @Bean("connectionFactorySender")
    public JndiObjectFactoryBean jndiObjectFactoryBean() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
        jndiObjectFactoryBean.setJndiName(jmsSenderConnectionFactory);
        jndiObjectFactoryBean.setExposeAccessContext(true);
        jndiObjectFactoryBean.setLookupOnStartup(false);
        jndiObjectFactoryBean.setProxyInterface(ConnectionFactory.class);
        return jndiObjectFactoryBean;
    }


    @Bean("jmsDestinationResolverSender")
    public JndiDestinationResolver jndiDestinationResolver() {
        JndiDestinationResolver jndiDestinationResolver = new JndiDestinationResolver();
        jndiDestinationResolver.setJndiTemplate(jndiTemplate());
        return jndiDestinationResolver;
    }



    @Bean("jmsTemplateSender")
    public JmsTemplate jmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory((ConnectionFactory) jndiObjectFactoryBean().getObject());
        jmsTemplate.setDestinationResolver(jndiDestinationResolver());
        return jmsTemplate;
    }

    @Bean("jmsDestinationSender")
    public JndiObjectFactoryBean jndiObjectFactoryQueueBean() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
        jndiObjectFactoryBean.setJndiName(jmsSenderDestination);
        return jndiObjectFactoryBean;
    }

    @Bean("jmsConfigSender")
    public JmsConfiguration jmsConfiguration() {
        JmsConfiguration jmsConfiguration = new JmsConfiguration();
        jmsConfiguration.setConnectionFactory((ConnectionFactory) jndiObjectFactoryBean().getObject());
        jmsConfiguration.setDestinationResolver(jndiDestinationResolver());
        jmsConfiguration.setTaskExecutor(threadPoolTaskExecutor());
        jmsConfiguration.setConcurrentConsumers(1);
        return jmsConfiguration;
    }

    @Bean("jmsInterceptStrategySender")
    public WeblogicSecurityInterceptStrategySender weblogicSecurityInterceptStrategySender() {
        WeblogicSecurityInterceptStrategySender weblogicSecurityInterceptStrategySender = new WeblogicSecurityInterceptStrategySender();
        weblogicSecurityInterceptStrategySender.setWeblogicSecurityBeanSender(weblogicSecurityBeanSender());
        return weblogicSecurityInterceptStrategySender;
    }


}
